package com.dobby.logger.system.thread;

import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;
import java.util.concurrent.*;

/**
 * Created by liguoqing on 2016/3/11.
 */
public class ExecutorServiceTest {

    private static final Logger logger = LoggerFactory.getLogger(ExecutorServiceTest.class);

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        logger.info("========> 程序开始");
        Date startDate = new Date();
        int taskNum = 5;

        // 创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(taskNum);
        // 创建有多个返回值得任务
        List<Future> list = Lists.newArrayList();
        for (int i = 0; i < taskNum; i++) {
            Callable callable = new MyCallable(i + "");
            // 执行任务并获取future对象
            Future future = executorService.submit(callable);
            list.add(future);
        }
        // 关闭线程池
        executorService.shutdown();


        for(Future future : list) {
            logger.info("=======>" + future.get().toString());
        }

        Date endDate = new Date();
        logger.info("===========> 程序运行结束");
        logger.info("程序运行时间：" + (endDate.getTime()-startDate.getTime()) + "毫秒");
    }
}
